package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.LoginHistory;
import com.daoliuhe.scaffold.service.LoginHistoryService;
import com.daoliuhe.scaffold.tools.Constants;
import com.daoliuhe.scaffold.tools.DateUtil;
import com.daoliuhe.scaffold.tools.ExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("loginHistory")
public class LoginHistoryController {

    private static final Logger logger = LoggerFactory.getLogger(LoginHistoryController.class);

    @Autowired
    LoginHistoryService loginHistoryService;

    @RequestMapping("/list")
    public ModelAndView list(LoginHistory history) throws UnsupportedEncodingException {
        logger.info("list,history:{}", history);
        ModelAndView mav = new ModelAndView("admin/loginHistory/list");
        mav.addAllObjects(loginHistoryService.getPageData(history));
        mav.addObject("entity", history);
        return mav;
    }

    @RequestMapping("/export")
    public void getExportedDefect(HttpServletResponse response, LoginHistory history){
        logger.info("getExportedDefect,history:{}", history);
        OutputStream os = null;
        try {
            List<LoginHistory> exported = loginHistoryService.getExportData(history);
            List<String[]> list = new ArrayList<String[]>();
            String[] title = { "用户", "邮箱", "部门名称", "类型", "IP地址","时间"};
            list.add(title);
            for (LoginHistory l : exported) {
                String type = l.getType();
                String typeName = "";
                if (Constants.TYPE_LOGIN.equals(type)) {
                    typeName = "登录";
                } else {
                    typeName = "登出";
                }
                String[] row = {
                    l.getUserIdName(),
                    l.getEmail(),
                    l.getDeptName(),
                    typeName,
                    l.getAddress(),
                    l.getCreateTime()
                };
                list.add(row);
            }

            Workbook workbook = ExcelUtil.createWorkbook(list);

            String sysdate = DateUtil.format(new Date());
            String fileName = "导出结果"+sysdate+".xls";
            response.setContentType("application/x-download");
            response.addHeader("Content-Disposition", new String(("filename=" + fileName).getBytes("GBK"), "ISO-8859-1"));
            os = response.getOutputStream();
            workbook.write(os);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("e, Message:{}, LocalizedMessage:{}, Cause:{}", e.getMessage(), e.getLocalizedMessage(), e.getCause());
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
